﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using NIEN_LUAN.Db;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormTimChiTietNCC : Form
    {
        private DataContentDataContext db = new DataContentDataContext();
        private FormLapPhieuNhap frmLapPhieuNhap;
        private FormTimKiemPhieuNhap frmTimKiemPhieuNhap;

        public FormTimChiTietNCC()
        {
            InitializeComponent();
        }

        public FormTimChiTietNCC(FormLapPhieuNhap frm)
        {
            this.frmLapPhieuNhap = frm;
            InitializeComponent();

            this.btnTimNCC.Enabled = true;
            this.btnAdd.Enabled = false;
            this.btnhuyCTNCC.Enabled = false;
            this.btnThemCTNCC.Enabled = false;
            this.btnThoatCTNCC.Enabled = true;
        }

        public FormTimChiTietNCC(FormTimKiemPhieuNhap frm)
        {
            this.frmTimKiemPhieuNhap = frm;
            InitializeComponent();          
        }

        private void btnThemNCC_Click(object sender, EventArgs e)
        {
            //Form frmThemChiTietNCC = new FormChiTietNhaCungCap();
            //frmThemChiTietNCC.ShowDialog();
        }

        private void btnhuyCTNCC_Click(object sender, EventArgs e)
        {
            this.txtTenNCC.ResetText();
            this.txtTenNCC.Focus();            
        }

        private void btnThoatCTNCC_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnTimNCC_Click(object sender, EventArgs e)
        {
            var danhsachnhacungcaptk = (from m in db.NHACUNGCAPs
                                       where m.TenNhaCungCap.StartsWith(this.txtTenNCC.Text)
                                       select m).ToList();
            this.nHACUNGCAPBindingSource.DataSource = danhsachnhacungcaptk;

            //this.btnTimNCC.Enabled = true;
            //this.btnAdd.Enabled = true;
            //this.btnThemCTNCC.Enabled = true;
            //this.btnhuyCTNCC.Enabled = true;
            //this.btnThoatCTNCC.Enabled = true;
        }

        private void FormTimChiTietNCC_Load(object sender, EventArgs e)
        {
            this.dataGridViewDSNCC.AutoGenerateColumns = false;
            var chitietncc = (from ncc in db.NHACUNGCAPs
                              orderby ncc.MaNhaCungCap ascending
                              select ncc
                                  ).ToList();
            this.nHACUNGCAPBindingSource.DataSource = chitietncc;
        }
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            var nhacungcap = this.nHACUNGCAPBindingSource.Current as NHACUNGCAP;
            if (nhacungcap != null)
            {
                // this.frmLapChungTu.txtMaKH.Text = khachhang.MaKhachHang;
                if (frmTimKiemPhieuNhap != null)
                {
                    this.DialogResult = DialogResult.OK;
                    this.frmTimKiemPhieuNhap.comboBoxNCC.Text = nhacungcap.MaNhaCungCap;
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("Chưa chọn nhà cung cấp");
            }
        }
        
        private void btnThemCTNCC_Click(object sender, EventArgs e)
        {
            var kq = new FormChiTietNhaCungCap(this).ShowDialog();
            if (kq == DialogResult.OK)
            {
                MessageBox.Show("Đã thêm thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }        
    }
}
